package promotion.rest;

import org.restlet.Request;
import org.restlet.Response;
import org.restlet.Restlet;
import org.restlet.data.MediaType;
import org.restlet.representation.StringRepresentation;
import org.restlet.routing.Router;
import promotion.resource.PromotionResource;

/**
 * Classe qui configure les uris associés aux ressources
 * @author Lou Ferrand
 */
public class Application extends org.restlet.Application {

    private final Router router;

    public Application(Router router) {
        this.router = router;
    }

    @Override
    public synchronized Restlet createInboundRoot() {
        router.setContext(getContext());

        /**
         * Association d'une uri à la récupération d'une promotion
         */
        router.attach("/promotions/{idPromotion}", PromotionResource.class);

        Restlet mainpage = new Restlet() {

            @Override
            public void handle(Request request, Response response) {
                StringBuilder stringBuilder = new StringBuilder();

                stringBuilder.append("<html>");
                stringBuilder.append("<head><title>Projet XML</title></head>");
                stringBuilder.append("<body>");
                stringBuilder.append("<h1>Service Web de Lille</h1>");
                stringBuilder.append("</body>");
                stringBuilder.append("</html>");

                response.setEntity(new StringRepresentation(stringBuilder.toString(), MediaType.TEXT_HTML));

            }
        };
        router.attach("/", mainpage);
        return router;
    }
}
